home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / XLIB06.ZIP / XLIBREF2.DOC < prev    next >
Text File  |  1993-10-07  |  37KB  |  1,098 lines

  1. PART 2 of 2
  2. -----------------------------------------------------------------------------
  3.  
  4.   *********      XLIB - Mode X graphics library           ****************
  5.   *********                                               ****************
  6.   ********* Written By Themie Gouthas                     ****************
  7.   *********                                               ****************
  8.   ********* egg@dstos3.dsto.gov.au                        ****************
  9.   ********* teg@bart.dsto.gov.au                          ****************
  10.  
  11.       Some of the code in this library has been contributed by :
  12.  
  13.            Matthew MacKenzie - matm@eng.umd.edu
  14.  
  15.                 and others. See individual modules.
  16.  
  17.          I informally reserve all rights to the code in XLIB
  18.      Rights to contributed code is also assumed to be reserved by
  19.                    the original authors.
  20. -----------------------------------------------------------------------------
  21. MODULE  XCLIPPBM   note: VERY SIMILAR to XPBMCLIP
  22. This module implements blits of clipped planar bitmaps.  Blits are
  23. clipped to pixels, both horizontally.  This makes the unmasked blit
  24. function here slightly slower than the equivalent functions in the
  25. XPBMCLIP module.
  26. --------------------------------------------------------------------------
  27.                                  XCLIPPBM:
  28.                              Blits and Pieces
  29.                            by Matthew MacKenzie
  30.  
  31. The XCLIPPBM module contains clipping versions of two of the three routines
  32. in the XPBITMAP module:
  33.   o  x_clip_pbm transfers a planar bitmap to the screen, clipping off any
  34.      part outside a bounding box.
  35.   o  x_clip_masked_pbm does the same thing, but transfers only nonzero
  36.      pixels.
  37.  
  38.     The planar bitmap format is described elsewhere.  Here we will look at
  39. the clipping itself, since it is the only distinguishing feature of this
  40. module.
  41.     The bounding box is made up of four integers, TopBound, BottomBound,
  42. LeftBound, and RightBound.  Unlike most global variables in Xlib, these are
  43. meant to be written to.  In fact, they start out uninitialized.  Be sure to
  44. set them before you try plotting any clipped bitmaps.
  45.     Note that these are not the same variables which are used in the other
  46. clipping modules in Xlib.  This is because the two systems are incompatible:
  47. the other modules clip horizontally to columns while this one clips to
  48. pixels.  As you might have guessed, those functions and these were developed
  49. in different hemispheres of the planet...
  50.     If it's any consolation, this does give you two independent
  51. bounding boxes to futz with, should the mood visit you.
  52.     Bitmaps cannot go outside the perimeter of the bounding box, but they
  53. can overlap it.  If TopBound equals BottomBound, for example, a horizontal
  54. slice of a bitmap may still be plotted.  It is safe to turn the box "inside
  55. out" to make sure nothing will be plotted -- this is the first thing each
  56. routine checks for.
  57.     To plot a bitmap, minus its zero pixels, minus anything outside the
  58. bounding box:
  59.  
  60. x_clip_masked_pbm (int X, int Y, int ScreenOffs, char far * Bitmap);
  61.  
  62.     The arguments are in the same order as those for x_put_masked_pbm in
  63. the module XPBITMAP.  The bounding box is relative to the given
  64. ScreenOffs(et).  This lets you perform page flipping without worrying about
  65. what screen you are clipping to -- it's always the current screen.  The
  66. bitmap itself, of course, is not affected; clipping is performed on-the-
  67. fly.  Both functions return an integer which indicates whether any part
  68. of the bitmap was inside the bounding box.  If the entire bitmap was
  69. outside, a 1 is returned; otherwise, a 0.
  70.     The third function in XPBITMAP, for which this module has no
  71. equivalent, copies from video RAM to system RAM.  The absence of such a
  72. routine may seem at first like a disadvantage -- but this, like so many
  73. things in this life, is an illusion.  You can use the unclipped routine,
  74. and clip the bitmap when you want to plot it back onto the screen.
  75.  
  76.   ASM SOURCES
  77.  
  78.      xclippbm.asm xclippbm.inc xlib.inc model.inc
  79.  
  80.   C HEADER FILE
  81.  
  82.      xclippbm.h
  83.  
  84.   EXPORTED VARIABLES
  85.  
  86.   TopBound - int
  87.   BottomBound - int
  88.   LeftBound - int
  89.   RightBound - int
  90.  
  91.   EXPORTED FUNCTIONS
  92.  
  93.   x_clip_pbm
  94.   ----------
  95.   C Prototype: extern int x_clip_pbm (int X, int Y, int ScreenOffs, char
  96.                                        far * Bitmap);
  97.  
  98.   Copies a planar bitmap from SRAM to VRAM, with clipping.  If the entire
  99.   bitmap turns out to be outside the bounding box, this function returns
  100.   a 1; otherwise it returns a 0.
  101.  
  102.   x_clip_masked_pbm
  103.   -----------------
  104.   C Prototype: extern int x_clip_masked_pbm (int X, int Y,
  105.                                    int ScreenOffs, char far * Bitmap);
  106.  
  107.   Copies a planar bitmap from SRAM to VRAM, with clipping -- 0 bytes
  108.   in the bitmap are not copied.  If the entire bitmap turns out to be
  109.   outside the bounding box, this function returns a 1; otherwise,
  110.   it returns a 0.
  111.  
  112.  
  113. --------------------------------------------------------------------------
  114. MODULE  XMOUSE
  115. --------------------------------------------------------------------------
  116. The XMOUSE module implements very basic mouse handling functions. The way
  117. in which it operates is by installing an event handler function during
  118. initialization which subsequently intercepts and processes mouse events and
  119. automatically updates status variables such as mouse position and button
  120. pressed status. It does not support the full functionality of:
  121.  
  122.   SPLIT SCREENS
  123.   SCROLLED WINDOWS
  124.   VIRTUAL WINDOWS
  125.  
  126. This was done to primarily prevent unecessary impedences to performance,
  127. since the mouse handler function has the potential to degrade performance.
  128. It also saves me alot of coding which I was too lazy to do.
  129.  
  130. Programs communicate with the mouse driver as with other devices, through
  131. an interrupt vector namely 33h. On generating an interrupt, the mouse driver
  132. expects a function number in AX and possibly other parameters in other
  133. registers and returns information via the registers. A brief description
  134. of the mouse functions follows:
  135.  
  136.           --------------------------------------
  137.  
  138.           MS Mouse Driver Functions
  139.  
  140.           Mouse Initialization                 0
  141.           Show Cursor                          1
  142.           Hide Cursor                          2
  143.           Get Mouse Position & Button Status   3
  144.           Set Mouse Cursor Position            4
  145.           Get Button Press Information         5
  146.           Get Button Release Information       6
  147.           Set Min/Max Horizontal Position      7
  148.           Set Min/Max Vertical Position        8
  149.           Define Graphics Cursor Block         9
  150.           Define Text Cursor                  10
  151.           Read Mouse Motion Counters          11
  152.           Define Event Handler                12
  153.           Light Pen Emulation Mode ON         13
  154.           Light Pen Emulation Mode OFF        14
  155.           Set Mouse Mickey/Pixel Ratio        15
  156.           Conditional Hide Cursor             16
  157.           Set Double-Speed Threshold          19
  158.           --------------------------------------
  159.  
  160. In practice only afew of these functions are used and even fewer when the
  161. mouse status is monitored by an event handler function such as is used in
  162. this module.
  163.  
  164. The most important thing to note when using the mouse module is that the
  165. mouse event handler must be removed before exiting the program. It is a good
  166. idea to have an exit function (see the C "atexit" function) and include the
  167. line "x_mouse_remove();" along with any other pre-exit cleanup code.
  168.  
  169. See also: XDETECT for mouse detection.
  170.  
  171.   ASM SOURCES
  172.  
  173.     xmouse.asm xlib.inc model.inc
  174.  
  175.   C HEADER FILE
  176.  
  177.     xmouse.h
  178.  
  179.   EXPORTED VARIABLES
  180.  
  181.    MouseInstalled    - WORD - Indicates whether mouse handler installed
  182.    MouseHidden       - WORD - Indicates whether mouse cursor is hidden
  183.    MouseButtonStatus - WORD - Holds the mouse button status
  184.    MouseX            - WORD - Current X position of mouse cursor
  185.    MouseY            - WORD - Current Y position of mouse cursor
  186.    MouseFrozen       - WORD - Disallows position updates if TRUE
  187.    MouseColor        - BYTE - The mouse cursors colour
  188.  
  189.   EXPORTED FUNCTIONS
  190.  
  191.   x_mouse_init
  192.   ------------
  193.  
  194.     C Prototype:  int x_mouse_init()
  195.  
  196.   Initialize the mouse driver functions and install the mouse event handler
  197.   function. This is the first function you must call before using any of the
  198.   mouse functions. This mouse code uses the fastest possible techniques to
  199.   save and restore mouse backgrounds and to draw the mouse cursor.
  200.  
  201.   WARNING: This function uses and updates "NonVisual_Offset" to allocate
  202.        video ram for the saved mouse background.
  203.  
  204.   LIMITATIONS: No clipping is supported horizontally for the mouse cursor
  205.            No validity checking is performed for NonVisual_Offs
  206.  
  207.   **WARNING** You must Hide or at least Freeze the mouse cursor while drawing
  208.           using any of the other XLIB modules since the mouse handler may
  209.           modify vga register settings at any time. VGA register settings
  210.           are not preserved which will result in unpredictable drawing
  211.           behavior. If you know the drawing will occur away from the
  212.           mouse cursor set MouseFrozen to TRUE (1), do your drawing
  213.           then set it to FALSE (0). Alternatively call "x_hide_mouse",
  214.           perform your drawing and then call "x_show_mouse". Another
  215.           alternative is to disable interrupts while drawing but usually
  216.           drawing takes up alot of time and having interrupts disabled
  217.           for too long is not a good idea.
  218.  
  219.   x_define_mouse_cursor
  220.   ---------------------
  221.  
  222.   C Prototype:
  223.     void x_define_mouse_cursor(char far *MouseDef, unsigned char MouseColor)
  224.  
  225.     MouseDef - a pointer to 14 characters containing a bitmask for all the
  226.            cursor's rows.
  227.     MouseColor - The colour to use when drawing the mouse cursor.
  228.  
  229.   Define a mouse cursor shape for use in subsequent cursor redraws. XMouse
  230.   has a hardwired mouse cursor size of 8 pixels across by 14 pixels down.
  231.  
  232.   WARNING: This function assumes MouseDef points to 14 bytes.
  233.  
  234.   Note: Bit order is in reverse. ie bit 7 represents pixel 0 ..
  235.     bit 0 represents pixel 7 in each "MouseDef" byte.
  236.  
  237.   x_show_mouse
  238.   ------------
  239.  
  240.   C Prototype:  void x_show_mouse()
  241.  
  242.   Makes the cursor visible if it was previously hidden.
  243.   See Also: "x_hide_mouse".
  244.  
  245.   x_hide_mouse
  246.   ------------
  247.  
  248.   C Prototype:  void x_hide_mouse()
  249.  
  250.   Makes the cursor hidden if it was previously visible.
  251.   See Also: "x_show_mouse".
  252.  
  253.   x_remove_mouse
  254.   --------------
  255.  
  256.   C Prototype:  void x_remove_mouse()
  257.  
  258.   Stop mouse event handling and remove the mouse handler.
  259.  
  260.   NOTE: This function MUST be called before quitting the program if
  261.        a mouse handler has been installed
  262.  
  263.   x_position_mouse
  264.   ----------------
  265.  
  266.   C Prototype  void x_position_mouse(int x, int y)
  267.  
  268.   Positions the mouse cursor at the specified location
  269.  
  270.   x_update_mouse
  271.   --------------
  272.  
  273.   C Prototype:  void x_update_mouse()
  274.  
  275.   Forces the mouse position to be updated and cursor to be redrawn.
  276.   Note: this function is useful when you have set "MouseFrozen" to true.
  277.   Allows the cursor position to be updated manually rather than
  278.   automatically by the installed handler.
  279.  
  280. --------------------------------------------------------------------------
  281. MODULE  XCIRCLE
  282. --------------------------------------------------------------------------
  283.                 XCIRCLE:
  284.               Wheel Have to See About That
  285.              by Matthew MacKenzie
  286.  
  287. The XCIRCLE module contains two functions, neither of which should be
  288. a big mystery:
  289.   o  x_circle, oddly enough, draws a circle.
  290.   o  x_filled_circle does too, only the circle is filled (in some
  291.      libraries this is called a disc).
  292.  
  293.     The word `circle' here refers to a round thing which is as many
  294. pixels tall as across.  It only looks like a circle in 320x240 mode --
  295. the original mode X -- and in 376x282 mode.
  296.     In both functions, the circle is specified by the coordinates of the
  297. upper-left-hand corner of the smallest box which holds it, and the
  298. diameter.  Some circle functions have you specify a center point;
  299. this system is kind of odd because a circle with an even diameter does
  300. not have a particular pixel for a center.  Every circle, on the other
  301. hand, has a box with an upper-left corner.
  302.     No bounds are checked.  A diameter of zero will draw nothing, and
  303. a negative diameter will blow your VGA board into hundreds of thousands
  304. of tiny little smoldering fragments.  Neither function supports clipping.
  305.     The calculation of the circle is based on an algorithm described
  306. by Michael P. Lindner in a letter to the editor on page 8 of Dr. Dobb's
  307. Journal #169 (October 1990).  The algorithm has been rearranged to
  308. allow drawing and moving the plots in the eight octants to be performed
  309. in one step, so that each pixel does not have to be loaded into the CPU
  310. twice.  x_filled_circle does not take advantage of this optimization
  311. because it handles different parts of each plot at different times.
  312.  
  313.   ASM SOURCES
  314.  
  315.   xcircle.asm xcircle.inc xlib.inc model.inc
  316.  
  317.   C HEADER FILE
  318.  
  319.   xcircle.h
  320.  
  321.   EXPORTED FUNCTIONS
  322.  
  323.   x_circle
  324.   --------
  325.   C Prototype: extern void x_circle (WORD Left, WORD Top, WORD Diameter,
  326.                      WORD Color, WORD ScreenOffs);
  327.  
  328.   Draws a circle with the given upper-left-hand corner and diameter,
  329.   which are given in pixels.
  330.  
  331.  
  332.   x_filled_circle
  333.   ---------------
  334.   C Prototype: extern void x_filled_circle (WORD Left, WORD Top,
  335.                 WORD Diameter, WORD Color, WORD ScreenOffs);
  336.  
  337.   Draws a filled circle with the given upper-left-hand corner and
  338.   diameter.
  339.  
  340.  
  341. --------------------------------------------------------------------------
  342. MODULE XDETECT
  343. --------------------------------------------------------------------------
  344.  
  345.   This module implements a set of functions to detect the PC's hardware
  346.   configuration.
  347.  
  348.   ASM SOURCES
  349.  
  350.     xdetect.asm xdetect.inc model.inc
  351.  
  352.   C HEADER FILE
  353.  
  354.     xdetect.h
  355.  
  356.   EXPORTED MACROS
  357.  
  358.     I8086   0
  359.     I80186  1
  360.     I80286  2
  361.     I80386  3
  362.  
  363.     NoGraphics 0
  364.     MDA        1
  365.     CGA        2
  366.     EGAMono    3
  367.     EGAColor   4
  368.     VGAMono    5
  369.     VGAColor   6
  370.     MCGAMono   7
  371.     MCGAColor  8
  372.  
  373.     BUS_MOUSE     1
  374.     SERIAL_MOUSE  2
  375.     INPORT_MOUSE  3
  376.     PS2_MOUSE     4
  377.     HP_MOUSE      5
  378.  
  379.  
  380.   EXPORT VARIABLES
  381.  
  382.   MouseButtonCount  - WORD - The number of buttons on the detected mouse
  383.   MouseVersion      - WORD - Mouse driver version (High byte = Major version
  384.                  Low byte = minor version)
  385.   MouseType         - BYTE - The mouse type
  386.   MouseIRQ          - BYTE - The IRQ number used by the mouse driver
  387.  
  388.   EXPORT FUNCTIONS
  389.  
  390.  
  391.   x_graphics_card
  392.   ---------------
  393.   C Prototype: extern int x_graphics_card();
  394.  
  395.   This function returns the type of graphics card installed. See defines
  396.   above.
  397.  
  398.   x_processor
  399.   -----------
  400.   C Prototype: extern int x_processor();
  401.  
  402.   This function returns the type of processor installed. A 486 registers
  403.   as a 386. See defines above.
  404.  
  405.   x_coprocessor
  406.   -------------
  407.   C Prototype: extern int x_coprocessor();
  408.  
  409.   This function returns 1 of a numeric co-processor is present, 0 if not.
  410.   The type is not detected but it's mnot really necessary as the processor
  411.   type usually determines the numeric coprocessor type
  412.  
  413.   x_mousedriver
  414.   -------------
  415.   C Prototype: extern int x_mousedriver();
  416.  
  417.   This function returns 1 of a mouse driver is installed, 0 otherwise.
  418.   If a mouse driver is detected the mouse related variable (above) are
  419.   set accordingly.
  420.  
  421. --------------------------------------------------------------------------
  422. MODULE XFILEIO
  423. --------------------------------------------------------------------------
  424.  
  425.   Handle based file I/O functions.
  426.  
  427.   See any good DOS programming reference for more information on int 21h
  428.   DOS services.
  429.  
  430.   ASM SOURCES
  431.  
  432.     xfileio.asm xfileio.inc model.inc
  433.  
  434.   C HEADER FILE
  435.  
  436.     xfileio.h
  437.  
  438.   EXPORTED MACROS
  439.  
  440.   file access modes
  441.  
  442.     F_RDONLY
  443.     F_WRONLY
  444.     F_RDWR
  445.  
  446.   seek codes
  447.  
  448.     SEEK_START
  449.     SEEK_CURR
  450.     SEEK_END
  451.  
  452.   file error value
  453.  
  454.     FILE_ERR
  455.  
  456.   EXPORT FUNCTIONS
  457.  
  458.   f_open
  459.   ------
  460.   C Prototype: extern int f_open(char * filename, char access);
  461.  
  462.   Opens a file according to the access char:
  463.  
  464.     F_RDONLY = read only   - If doesnt exist return error
  465.     F_WRONLY = write only  - If doesnt exist create it otherwise clear it
  466.     F_RDWR   = read/write  - If doesnt exist create it
  467.  
  468.   Returns the file handle on success, FILE_ERR on failure
  469.  
  470.  
  471.   f_close
  472.   -------
  473.  
  474.   C Prototype:  extern int f_close(int handle);
  475.  
  476.   Closes the file associated with the specified handle
  477.  
  478.   Returns 0 on success, FILE_ERR on failure
  479.  
  480.  
  481.   f_read
  482.   ------
  483.  
  484.   C Prototype:
  485.  
  486.     extern int f_read(int handle,char near * buffer, int count);
  487.  
  488.   Reads a block of count bytes from the file specified by the handle
  489.   into the near buffer
  490.  
  491.   Returns count on success, FILE_ERR on failure
  492.  
  493.   f_readfar
  494.   ---------
  495.  
  496.   C Prototype:
  497.  
  498.     extern int f_readfar(int handle,char far * buffer, int count);
  499.  
  500.   Reads a block of count bytes from the file specified by the handle
  501.   into the far buffer
  502.  
  503.   Returns count on success, FILE_ERR on failure
  504.  
  505.  
  506.   f_write
  507.   -------
  508.  
  509.   C Prototype: extern int f_write(int handle, char near * buffer, int count);
  510.  
  511.   Writes a block of count bytes to the file specified by the handle
  512.   from the near buffer
  513.  
  514.   Returns count on success, FILE_ERR on failure
  515.  
  516.   f_writefar
  517.   ----------
  518.  
  519.   C Prototype: extern int f_write(int handle, char far * buffer, int count);
  520.  
  521.   Writes a block of count bytes to the file specified by the handle
  522.   from the far buffer
  523.  
  524.   Returns count on success, FILE_ERR on failure
  525.  
  526.  
  527.   f_seek
  528.   ------
  529.  
  530.   C Prototype: extern long int f_seek(int handle, long int position,
  531.                       char method_code)
  532.  
  533.   Moves the file pointer according to the position and method code
  534.  
  535.   Returns file pointer position on success, FILE_ERR on failure
  536.  
  537.  
  538.   f_filelength
  539.   ------------
  540.  
  541.   C Prototype:
  542.  
  543.     extern long int f_filelength(int handle)
  544.  
  545.   Returns the length of the file associated with the specified handle
  546.  
  547.   Returns file length on success, FILE_ERR on failure
  548.  
  549.  
  550.   f_tell
  551.   ------
  552.  
  553.   C Prototype:
  554.  
  555.     extern long int f_tell(int handle)
  556.  
  557.  
  558.   Returns file pointer position on success, FILE_ERR on failure
  559.  
  560. --------------------------------------------------------------------------
  561. MODULE XRLETOOL
  562. --------------------------------------------------------------------------
  563.  
  564. This module implements a number of functions comprising an RLE encoding
  565. decoding system.
  566.  
  567. RLE stands for RUN LENGTH ENCODING. It is a quick simple data compression
  568. scheme which is commonly used for image data compression or compression
  569. of any data. Although not the most efficient system, it is fast, which is
  570. why it is used in image storage systems like PCX. This implementation is
  571. more efficient than the one used in PCX files because it uses 1 bit to
  572. identify a Run Length byte as opposed to two in PCX files, but more on this
  573. later.
  574.  
  575. This set of functions can be used to implement your own compressed image
  576. file format or for example compress game mapse for various levels etc.
  577. The uses are limited by your imagination.
  578.  
  579. I opted for trading off PCX RLE compatibility for the improved compression
  580. efficiency.
  581.  
  582. Here is how the data is un-compressed to give an idea of its structure.
  583.  
  584.  
  585. STEP 1 read a byte from the RLE compressed source buffer.
  586.  
  587. STEP 2 if has its high bit is set then the lower 7 bits represent the number
  588.        of times the next byte is to be repeated in the destination buffer.
  589.        if the count (lower 7 bits) is zero then
  590.       we have finished decoding goto STEP 5
  591.        else goto STEP 4
  592.  
  593. STEP 3 Read a data from the source buffer and copy it directly to the
  594.        destination buffer.
  595.        goto STEP 1
  596.  
  597. STEP 4 Read a data byte from the source buffer and copy it to the destination
  598.        buffer the number of times specified by step 2.
  599.        goto STEP 1
  600.  
  601. STEP 5 Stop, decoding done.
  602.  
  603. If the byte does not have the high bit set then the byte itself is transfered
  604.  to the destination buffer.
  605.  
  606. Data bytes that have the high bit already set and are unique in the input
  607.  stream are represented as a Run Length of 1 (ie 81 which includes high bit)
  608.  followed by the data byte.
  609.  
  610. If your original uncompressed data contains few consecutive bytes and most
  611. have high bit set (ie have values > 127) then your so called
  612. compressed data would require up to 2x the space of the uncompressed data,
  613. so be aware that the compression ratio is extremely variable depending on the
  614. type of data being compressed.
  615.  
  616. Apologies for this poor attempt at a description, but you can look up
  617. RLE in any good text. Alternatively, any text that describes the PCX file
  618. structure in any depth should have a section on RLE compression.
  619.  
  620.  
  621.  
  622.   ASM SOURCES
  623.  
  624.     xrletool.asm xrletool.inc model.inc
  625.  
  626.   C HEADER FILE
  627.  
  628.     xrletool.h
  629.  
  630.   EXPORTED MACROS
  631.  
  632.  
  633.   EXPORT FUNCTIONS
  634.  
  635.   x_buff_RLDecode
  636.   ---------------
  637.  
  638.    Expands an RLE compresses source buffer to a destination buffer.
  639.    returns the size of the resultant uncompressed data.
  640.  
  641.    C PROTOTYPE:
  642.  
  643.    extern unsigned int x_buff_RLDecode(char far * source_buff,
  644.                       char far * dest_buff);
  645.  
  646.    source_buff   - The buffer to compress
  647.    dest_buff     - The destination buffer
  648.  
  649.    WARNING: buffers must be pre allocated.
  650.  
  651.  
  652.    x_buff_RLEncode
  653.    ---------------
  654.  
  655.    RLE Compresses a source buffer to a destination buffer and returns
  656.    the size of the resultant compressed data.
  657.  
  658.    C PROTOTYPE:
  659.  
  660.     extern unsigned int x_buff_RLEncode(char far * source_buff,
  661.          char far * dest_buff,unsigned int count);
  662.  
  663.    source_buff   - The buffer to compress
  664.    dest_buff     - The destination buffer
  665.    count         - The size of the source data in bytes
  666.  
  667.    WARNING: buffers must be pre allocated.
  668.  
  669.    x_buff_RLE_size
  670.    ---------------
  671.  
  672.    Returns the size the input data would compress to.
  673.  
  674.    C PROTOTYPE:
  675.  
  676.     extern unsigned int x_buff_RLE_size(char far * source_buff,
  677.          unsigned int count);
  678.  
  679.    source_buff   - The uncompressed data buffer
  680.    count         - The size of the source data in bytes
  681.  
  682.  
  683.    x_file_RLEncode
  684.    ---------------
  685.  
  686.    RLE Compresses a source buffer to an output file returning
  687.    the size of the resultant compressed data or 0 if it fails.
  688.  
  689.    C PROTOTYPE:
  690.  
  691.    extern unsigned int x_file_RLEncode(int handle,
  692.      char far * source_buff,unsigned int count);
  693.  
  694.    source_buff   - The buffer to compress
  695.    handle        - The file handler
  696.    count         - The size of the source data in bytes
  697.  
  698.    x_file_RLDecode
  699.    ---------------
  700.  
  701.    Expands an RLE compresses file to a destination RAM buffer.
  702.    returns the size of the resultant uncompressed data.
  703.  
  704.    C PROTOTYPE:
  705.  
  706.     extern unsigned int x_buff_RLDecode(int handle,
  707.          char far * dest_buff);
  708.  
  709.    handle        - Input file handle
  710.    dest_buff     - The destination buffer
  711.  
  712.  
  713.  
  714.  
  715. --------------------------------------------------------------------------
  716. MODULE XPOLYGON
  717. --------------------------------------------------------------------------
  718.  
  719.   This module implements eneral filled convex polygon and triangle
  720.   functions
  721.  
  722.   C HEADER FILE
  723.  
  724.     xpolygon.h
  725.  
  726.   TYPE DEFS
  727.  
  728.   typedef struct {
  729.     int X;
  730.     int Y;
  731.   } far VERTEX;
  732.  
  733.  
  734.  
  735.   EXPORT FUNCTIONS
  736.  
  737.  
  738.   x_triangle
  739.   ------------
  740.   C Prototype:
  741.  
  742.   void x_triangle(int x0, int y0, int x1, int y1, int x2, int y2,
  743.          WORD color, WORD PageBase);
  744.  
  745.   This function draws a filled triangle which is clipped to the current
  746.   clipping window defined by TopClip,BottomClip,LeftClip,RightClip.
  747.   Remember: the X clipping variable are in BYTES not PIXELS so you
  748.     can only clip to 4 pixel byte boundaries.
  749.  
  750.  
  751.   x_polygon
  752.   ---------
  753.  
  754.   C Prototype:
  755.  
  756.   void x_polygon(VERTEX *vertices, int  num_vertices,
  757.          WORD color, WORD PageBase);
  758.  
  759.   This function is similar to the triangle function but draws
  760.   convex polygons. The vertices are supplied in the form of a FAR
  761.   pointer.
  762.  
  763.   NOTE: a convex polygon is one such that if you draw a line from
  764.   any two vertices, every point on that line will be within the
  765.   polygon.
  766.  
  767.   This function works by splitting up a polygon into its component
  768.   triangles and calling the triangle routine above to draw each one.
  769.   Performance is respectable but a custom polygon routine might be
  770.   faster.
  771.  
  772.  
  773. --------------------------------------------------------------------------
  774. MODULE XFILL
  775. --------------------------------------------------------------------------
  776.  
  777.   This module implements a couple of general purpose flood fill functions
  778.   functions
  779.  
  780.   C HEADER FILE
  781.  
  782.     xfill.h
  783.  
  784.  
  785.   EXPORT FUNCTIONS
  786.  
  787.  
  788.   x_flood_fill
  789.   ------------
  790.   C Prototype:
  791.  
  792.   int x_flood_fill(int x, int y, unsigned ofs, int color);
  793.  
  794.   This function performs the familiar flood filling used by many
  795.   paint programs and of course the Borland BGI's flood fill function.
  796.   The pixel at x,y and all adjacent pixels of the same color are filled
  797.   to the new color. Filling stops when there are no more adjacent pixels
  798.   of the original pixel's color. The function returns the number of
  799.   pixels that have been filled.
  800.  
  801.   x_boundary_fill
  802.   ------------
  803.   C Prototype:
  804.  
  805.  
  806.   int x_boundary_fill(int x,int y,unsigned ofs,int boundary,int color);
  807.  
  808.   This function is a variant of the flood fill described above. This
  809.   function, unlike the above function, can fill across color boundaries.
  810.   Filling stops when the area being filled is fully enclosed by pixels
  811.   of the color "boundary". Again, this function returns the number of
  812.   pixels filled.
  813.  
  814. -------------------------------------------------------------------------------
  815. MODULE VSYNC
  816. -------------------------------------------------------------------------------
  817.  
  818.                                VSYNC
  819.  
  820.             Simulated Vertical Retrace Interrupt Module
  821.  
  822.             by Tore Jahn Bastiansen <toreba@ifi.uio.no>
  823.  
  824.      Inspired by REND386 v3.01 by Dave Stampe and Bernie Roehl
  825.  
  826. The xvsync module uses timer 0 to simulate a vertical retrace interrupt.
  827. It's designed to significantly reduce the idle waiting time in Xlib.
  828. Why simulate the VRT interrupt ? Simply because a true VRT interrupt is
  829. not implemented on many VGA cards. Using a VRT interrupt as opposed to
  830. polling, can result in huge performance improvements for your code and
  831. help make animation much smoother than it would be using polling.
  832.  
  833. Normally xlib waits for vsync when x_page_flip, x_set_start_address or
  834. x_put_pal_??? is called. This waiting period could be better utilized to do
  835. housekeeping calculations or whatever. The x_put_pal_??? functions also
  836. doesn't work very smoothly in conjunction with other functions that wait for
  837. the vertical retrace since each function introduces its own VRT delay.
  838.  
  839. When using the vsync handler, the VRT delay is reduced to the absolute
  840. minumum which can result in a huge performance boost for your programs.
  841.  
  842. When using double buffering, you may still have to wait before drawing,
  843. but you could do as much other work as possible, like this:
  844.  
  845.         x_page_flip(...)
  846.         ...
  847.         <animate the palette>
  848.         <do some collision detection and 3D calculations>
  849.         <read the joystick>
  850.         ...
  851.         x_wait_start_address(); (Not needed with triple buffering)
  852.         ...
  853.         <draw next frame>
  854.         ...
  855.  
  856. ASM SOURCES
  857.  
  858.   xvsync.asm xmain.asm xvsync.inc xmain.inc
  859.  
  860. C HEADER FILE
  861.  
  862.   xvsync.h
  863.  
  864. EXPORTED VARIABLES
  865.  
  866. VsyncPeriod - WORD -time in 1.193 us between two vsyncs
  867.  
  868. TicksPerSecond - WORD - number of vsyncs per second
  869.  
  870. VsyncTimerInt - long - number of vsyncs since x_install_vsync_handler was
  871.      called. Nice for game timing.
  872.  
  873. EXPORTED FUNCTIONS
  874.  
  875. x_install_vsync_handler
  876. -----------------------
  877.  
  878. C Prototype:   void x_install_vsync_handler(int VrtsToSkip);
  879.  
  880. This function installs the vsync handler using timer 0. It's called
  881. about 100 microseconds before every vertical retrace.
  882.  
  883. The VrtsToSkip value (>=1) defines the delay in VRT's between consecutive
  884. physical screen start address changes, thus allowing you to limit the
  885. maximum frame rate for page flips in animation systems. The frame rate
  886. is calculated as Vertical refresh rate / VrtsToSkip, eg for
  887. 320x240 mode which refreshes at 60Hz a VrtsToSkip value of 3 will result
  888. in a maximum page flipping rate of 20Hz (frames per second)
  889.  
  890. WARNING:  Be sure to remove it before exiting.
  891.           When used with a debugger, the system clock may speed up.
  892.  
  893. x_remove_vsync_handler
  894. ----------------------
  895.  
  896. C Prototype:   void x_remove_vsync_handler
  897.  
  898. This routine _MUST_ be called before exiting (or aborting) the program,
  899. or your system will crash.
  900.  
  901. x_set_user_vsync_handler
  902. ------------------------
  903.  
  904. C Prototype:   void x_set_user_vsync_handler(void far (*f)());
  905.  
  906. Installs a user routine to be called once each vertical retrace. The user
  907. handler have its own stack of 256 bytes , so be careful with the stack
  908. checking option in BC.
  909. WARNING: This installs an interrupt driven handler, beware of the following:
  910.          Only 8086 registers are preserved. If you're using 386 code, save
  911.          all the 386 regs.
  912.          Don't do any drawing.
  913.          Don't call any DOS functions.
  914.  
  915. So why use it?
  916. Well, you can update global variables if you're careful. And it's nice for
  917. palette animation. You can even do fades while loading from disk. You
  918. should use this instead of installing your own int08h routine and chain
  919. to the original.
  920.  
  921. x_wait_start_addr
  922. -----------------
  923.  
  924. C Prototype:   void x_wait_start_addr(void)
  925.  
  926. You must call this function before drawing after a call to x_set_start_addr
  927. or x_page_flip when you are using the vsync handler and not using
  928. triple buffering.
  929.  
  930. MODULE XMAIN additions
  931.  
  932. EXPORTED VARIABLES
  933.  
  934. Page2_Offs - WORD - Offset in video ram of third virtual screen. Set by
  935.      x_triple_buffer.
  936.  
  937. WaitingPageOffs - WORD - Offset of page waiting to be invisible. Initially
  938.      set by x_set_triple_buffer but is updated by x_page_flip. This
  939.      variable is only used while triple buffering is on.
  940.  
  941. VsyncHandlerActive - WORD - Indicates whether the vsync handler is installed.
  942.  
  943. TripleBufferActive - WORD - Indicates whether triple-buffering is on.
  944.      Set by x_triple_buffer.
  945.  
  946. StartAddressFlag - WORD - This flag is set if there is a new start
  947.      address waiting to be set by the vsync handler.
  948.  
  949. WaitingStartLow - WORD -  Needed by vsync handler. Keep off.
  950. WaitingStartHigh - WORD - Needed by vsync handler. Keep off.
  951. WaitingPelPan - WORD -    Needed by vsync handler. Keep off.
  952.  
  953. VsyncPaletteStart - WORD - Start index of video DAC register to be
  954.      updated next vsync. Set by palette functions.
  955.  
  956. VsyncPaletteCount - WORD - Number of palette entries to be outed next
  957.      vsync. Set by palette functions.
  958.  
  959. VsyncPaletteBuffer - BYTE[768] - Buffer containing values for the next
  960.      update of the DAC.
  961.  
  962. EXPORTED FUNCTIONS
  963.  
  964. x_triple_buffer
  965. ----------------
  966.  
  967. C Prototype:   void x_triple_buffer(WORD PageHeight);
  968.  
  969. This function behaves like x_double_buffer, but when used with
  970. x_install_vsync_handler you can draw immediately after a page flip.
  971. When x_page_flip is called, VisiblePageOffs is set to the page that
  972. will be display next vsync. Until then, WaitingPageOffs will be displayed.
  973. You can draw to HiddenPageOffs .
  974.  
  975.  
  976.  
  977.  
  978. --------------------------------------------------------------------
  979. REFERENCE SECTION
  980. --------------------------------------------------------------------
  981.  
  982.  
  983. REFERENCES
  984. ----------
  985.  
  986. In my opinion Doctor Dobbs Journal is the best reference text for
  987. VGA Mode X graphics:
  988.  
  989. Issue 178 Jul 1991 : First reference to Mode X
  990. Article Abstract   : VGA's undocumented Mode X supports page flipping,
  991.              makes off screen memory available, has square pixels,
  992.              and increases performance by as muck as 4 times.
  993.  
  994. Issue 179 Aug 1991 : Continuation
  995. Article Abstract   : Michael discusses latches and VGA's undoccumented
  996.              Mode X.
  997.  
  998. Issue 181 Sep 1991 : Continuation
  999. Article Abstract   : Michael puts the moves on animation using VGA's 256
  1000.              colors.
  1001.  
  1002. Issue 184 Oct 1991 : First of a continuing series covering 3-D animation
  1003.              using VGA's Mode X. This series is still ongoing
  1004.              (October 1992)
  1005. Article Abstract   : Michael moves into 3-D animation, starting with basic
  1006.              polygon fills and page flips.
  1007.  
  1008.  
  1009. WHAT IS MODE X ?
  1010. ----------------
  1011.  
  1012. Mode X is a derrivative of the VGA's standard mode 13h (320x200 256 color).
  1013. It is a (family) of undocumented video modes that are created by tweaking
  1014. the VGA's registers. The beauty of mode X is that it offers several
  1015. benefits to the programmer:
  1016.  - Multiple graphice pages where mode 13h doesn't allowing for page flipping
  1017.    (also known as double buffering) and storage of images and data in
  1018.    offscreen video memory
  1019.  - A planar video ram organization which although more difficult to program,
  1020.    allows the VGA's plane-oriented hardware to be used to process pixels in
  1021.    parallel, improving performance by up to 4 times over mode 13h
  1022.  
  1023.    See issue 178-179 of D.D.J. for a full description of VGA's Mode X.
  1024.  
  1025. WHAT IS A SPLIT SCREEN ?
  1026. ------------------------
  1027.  
  1028. A split screen is a neat hardware feature offered by the EGA and VGA video
  1029. cards. A split screen is a mode of graphics operationin which the Hardware
  1030. splits the visual graphics screen horizontally and treats both halves as
  1031. individual screens each starting at different locations in video RAM.
  1032.  
  1033. The bottom half (which is usually referred to as the split screen) always
  1034. starts at address A000:0000 but the top half's starting address is user
  1035. definable.
  1036.  
  1037. The most common application of split screens in games is the status display
  1038. in scrolling games. Split screens make this sort of game simpler to program
  1039. because when the top half window is scrolled the programmer does not have to
  1040. worry about redrawing the bottom half.
  1041.  
  1042. WHAT IS DOUBLE BUFFERING ?
  1043. --------------------------
  1044.  
  1045. Double buffering (also known as page flipping) is the technique most often
  1046. used to do animation. it requires hardware that is capable of displaying
  1047. multiple graphics pages (or at least 2). Animation is achieved by drawing
  1048. an image in the non visible screen and then displaying the non visible
  1049. screen. Once the page has been flipped the process starts again. The next
  1050. frame of the animation is drawn on the non visible screen, the page is
  1051. flipped again etc.
  1052.  
  1053.  
  1054. WHAT IS TRIPPLE BUFFERING ?
  1055. --------------------------
  1056.  
  1057. Triple buffering is similar to double buffering in many ways, but it
  1058. relies on 3 pages being defined for animation. The main selling point
  1059. of triple buffering is that it eliminates the need to wait for the
  1060. vertical retrace to flip pages before drawing on the new page thus
  1061. alowing the programmer to start building the next animation frame
  1062. immediately after completing the current one. Heres how it works:
  1063.  
  1064. With double buffering, once you complete drawing the hidden page and
  1065. youre ready to flip pages, you have to wait for the VGA hardware to
  1066. actually flip the page (during the vertical retrace) before you start
  1067. drawing the next page otherwise you will be drawing on the visible page.
  1068.  
  1069. With triple buffering you cycle between three pages, thus the page you
  1070. draw on is guaranteed not to be visible. I know this is a poor
  1071. description but it really is quite simple
  1072.  
  1073. Triple buffering can acheive the fastest possible animation under the
  1074. right conditions but the draw back is that more video RAM is required. If
  1075. you wish to store bitmaps in video ram such as background tiles, double
  1076. buffering would be the better alternative.
  1077.  
  1078.  
  1079. WHAT IS MODE X ?
  1080. ----------------
  1081.  
  1082. Mode X is a derrivative of the VGA's standard mode 13h (320x200 256 color).
  1083. It is a (family) of undocumented video modes that are created by tweaking
  1084. the VGA's registers. The beauty of mode X is that it offers several
  1085. benefits to the programmer:
  1086.  - Multiple graphice pages where mode 13h doesn't allowing for page flipping
  1087.    (also known as double buffering) and storage of images and data in
  1088.    offscreen video memory
  1089.  - A planar video ram organization which although more difficult to program,
  1090.    allows the VGA's plane-oriented hardware to be used to process pixels in
  1091.    parallel, improving performance by up to 4 times over mode 13h
  1092.  
  1093.    Again see D.D.J. for an in depth discussion of animation using Mode X.
  1094.  
  1095.    -----------------------------------------------------------------------
  1096.  
  1097.  
  1098.